<template>
	<view class="content">
		<!-- 卡片简单信息 -->
		<view class="card-mainInfo">
			<view class="line1">
				<view class="card-money"><view class="card-money1">卡内金额</view><view class="card-money2">{{cardMoney}}</view></view>
				<view class="cross-money"><view class="cross-money1">过渡金额</view><view class="cross-money2">{{crossMoney}}</view></view>
				<view class="card-mode"><view class="card-mode1">卡状态</view><view class="card-mode2">{{cardMode}}</view></view>
				<view class="card-mode" @click="setFullroomname()"><view class="card-mode1">寝室电量</view><view class="card-mode2">{{roomNum}}</view></view>
			</view>
			<view class="line2">
				<view class="real-money"><view class="real-money1">实际金额</view><view class="real-money2">{{realMoney}}</view></view>
			</view>
		</view>
		<!-- 功能区 -->
		<view class="capabilitie">
			<view class="item" v-for="(item, index) in funtexts" :key="index" @click="onClickFun(index)">
				<image class="fun-img" v-bind:src="funimages[index]" mode=""></image>
				<text class="text-item">{{item}}</text>
				<image class="arrow-right-img" src="../../static/arrowRight.png" mode=""></image>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {mixin} from '../../mixin.js'
	import {AES_Encryptecb} from './ECB.js'
	export default {
		data() {
			return {
				isPull:false,
				user:'',
				password:'',
				cardMoney:"NaN",
				crossMoney:"NaN",
				cardMode:"NaN",
				realMoney:"NaN",
				roomNum:'NaN',
				fullroomname:'',//寝室号
				funtexts:["基本信息","转账充值","修改密码","流水查询","登记挂失","补助流水","电量充值","菜鸟身份码"],
				funimages:[
					"../../static/jibenxinxi.png",
					"../../static/zhuanzhangchongzhi.png",
					"../../static/xiugaimima.png",
					"../../static/liushuichaxun.png",
					"../../static/dengjiguashi.png",
					"../../static/buzhuliushui.png",
					"../../static/power.png",
					"../../static/shikaxinxi.png",
					],
				funUrls:[
					"basicInfo/basicInfo",
					"toMoney/toMoney",
					"changePwd/changePwd",
					"searchBill/searchBill",
					"registerLost/registerLost",
					"grant/grant",
					"toPower/toPower",
					"pickMessage/pickMessage"
				],
				userMsg:'',
				apis:[//ykt.ccufe.com 127.0.0.1
					"http://ykt.ccufe.com:8070/Api/Account/SignIn/",//登录API
					"http://ykt.ccufe.com:8070/Api/Card/GetCardEaccInfo"//获取基本信息API
				]
				
			}
		},
		mixins:[mixin],
		onPullDownRefresh(){
			this.isPull=true
			this.getMoneyInfo()
			this.getRoomNum()
		},
		onLoad() {
			
			this.password = uni.getStorageSync("pwd")
			this.user = uni.getStorageSync("user")
			console.log("------1--------"+this.password + "   "+ this.user)
			//未登录
			if(this.password==''){
				uni.reLaunch({
					url:'../index/login/login'
				})
			}else{
				let timeMsg = uni.getStorageSync("timeMsg")
				let userMsg = uni.getStorageSync("msg")
				let nowTime = Number(new Date())
				this.getRoomNum()//获取寝室电量
				if(timeMsg=='' || userMsg=='')
					this.getUserMsg()
				else if((nowTime - timeMsg) > 1700000){//1700秒 ~28分钟
					console.log(nowTime - timeMsg)
					this.getUserMsg()
				}else{
					this.getMoneyInfo()
				}
			}

		},
		methods: {
			getUserMsg(){
				///
				uni.showLoading({
					title:"用户凭证过期\n正在刷新...",
					mask:true
				});
				uni.request({
					//http://ykt.ccufe.com:8070
					url:this.apis[0],
					method:"POST",
					header:{
						'Cache-Control':'no-cache',
						'Accept':'*/*',
						'Accept-Encoding':'gzip, deflate, br',
						'Connection':'keep-alive',
						'Content-Type':'application/x-www-form-urlencoded',
						'Content-Length':'154'
					},
					data:{
						'account':uni.getStorageSync("user"),
						'clientmark':uni.getStorageSync("dev"),
						'clienttype':"Android",
						'password':uni.getStorageSync("pwd"),
						'schoolcode':"ccufe",
						'signtype':"SynSno",
						'sign':uni.getStorageSync("sign")
					},
					//timeout:4000,
					success:res=>{
						console.log(res)
						if(res.data.success==true){
							// uni.showToast({
							// 	title:"获取成功"
							// })
							//console.log("蔬菜")
							this.userMsg = res.data.msg
							uni.setStorageSync("timeMsg",Number(new Date()))
							uni.setStorage({
								key:"msg",
								data:this.userMsg,
								success:res=>{
									// uni.showToast({
									// 	title:"保存msg成功"
									// })
									this.getMoneyInfo()
								}
							})
							
						}
						
					},
					fail:err=>{
						
					},
					complete:()=>{
						uni.hideLoading();
					}
				})
				///
			},
			//进入具体的功能页面
			onClickFun(e){
				uni.navigateTo({
					url:this.funUrls[e],
					animationType:"pop-in",
					animationDuration:200
				})
			},
			setFullroomname(){
				uni.navigateTo({
					url:'../my/setRoom/setRoom'
				})
			},
			//获取寝室电量
			getRoomNum(){
				if(uni.getStorageSync('room')==''){
					this.roomNum = '未设置'
					return
				}
				let roomO = {
					sno: '4122013603',
					xiaoquId: '',
					loudongId: '',
					roomId: '',
					fullroomname: uni.getStorageSync('room'),
				}
				
				uni.request({
					url:'http://vpn.tdhzyun.com/tdwx-fr/ywcl/getRoomList',
					method:"POST",
					headers: {
					    Host: 'vpn.tdhzyun.com',
					    Connection: 'keep-alive',
					    Accept: 'application/json, text/plain, */*',
					    accessToken: '',
					    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36',
					    'Content-Type': 'application/json',
					    Origin: 'http://vpn.tdhzyun.com',
					    Referer: 'http://vpn.tdhzyun.com/person',
					    'Accept-Language': 'zh-CN,zh;q=0.9',
					    'Accept-Encoding': 'gzip, deflate',
					    'Content-Length': '87'
					  },
					  data: {
					    sno: '4122013603',
					    xiaoquId: '',
					    loudongId: '',
					    roomId: '',
					    fullroomname: uni.getStorageSync('room'),
						sign: AES_Encryptecb(JSON.stringify(roomO)+"1234567890ABCDEF"+this.getMd5(JSON.stringify(roomO)))
					  },
					  success:res=>{
						  console.log(res)
						  if(res.statusCode==200){
							  let obj = JSON.parse(this.getAES_Decryptecb(res.data.returnObject)).list[0]
							  if(obj==undefined){return}
							  let num =(obj.allamp - obj.usedamp).toFixed(2)
							  //console.log(num)
							  this.roomNum = num
						  }
					  },
					err:e=>{
						console.log(e)
					}
				})
			},
			//刷新金额等数据
			getMoneyInfo(){
				// console.log('--6--')
				const params = {
					"clienttype": "Android",
					"iplanetdirectorypro": uni.getStorageSync("msg"),
					"schoolcode": "ccufe"
				}
				uni.request({
					url:this.apis[1],
					method:"POST",
					header:{
						//'User-Agent':'Dalvik/2.1.0 (Linux; U; Android 12; DBY-W09 Build/HUAWEIDBY-W09)',
						'Cache-Control':'no-cache',
						'Accept':'*/*',
						'Accept-Encoding':'gzip',
						'Connection':'keep-alive',
						//'cookie':'ASP.NET_SessionId=uikvmjwffvfnclwb1kq1dwdx',
						'Content-Type':'application/x-www-form-urlencoded',
						'Content-Length':'166'
					},
					data:{
						"clienttype": "Android",
						"iplanetdirectorypro": uni.getStorageSync("msg"),
						"schoolcode": "ccufe",
						"sign": this.getMd5(this.getObjToStr(params))
					},
					success:res=>{
						console.log(res)
						if(res.data.success==true || res.data.success=='true'){
							
							uni.setStorageSync("name",res.data.obj.name),//姓名
							uni.setStorageSync("sno",res.data.obj.sno),//学号
							uni.setStorageSync("cardno",res.data.obj.cardno),//
							uni.setStorageSync("cardtype",res.data.obj.cardtype),//卡类型
							uni.setStorageSync("bankno",res.data.obj.bankno),//银行卡号
							uni.setStorageSync("bankbalance",String(res.data.obj.bankbalance)),//
							uni.setStorageSync("cardbalance",res.data.obj.cardbalance),//卡内金额
							uni.setStorageSync("tmpbalance",res.data.obj.tmpbalance),//
							uni.setStorageSync("pretmpbalance",res.data.obj.pretmpbalance),//过渡金额
							uni.setStorageSync("showbankbalance",res.data.obj.showbankbalance),//
							uni.setStorageSync("standingname",res.data.obj.standingname),//本科生
							uni.setStorageSync("deptname",res.data.obj.deptname),//班级
							uni.setStorageSync("cardstatus",res.data.obj.cardstatus),//卡状态
							uni.setStorageSync("state",res.data.obj.state)
							uni.setStorageSync("frozen",res.data.obj.frozen)
							uni.setStorageSync("eaccinfos",res.data.obj.eaccinfos)
	
							this.cardMoney=uni.getStorageSync("cardbalance"),
							this.crossMoney=uni.getStorageSync("pretmpbalance"),
							this.cardMode=uni.getStorageSync("cardstatus"),
							this.realMoney=Number(Number(this.cardMoney)+Number(this.crossMoney)).toFixed(2)
							
						}
					},
					complete:e=>{
						if(this.isPull == true){
							this.Pull = false
							uni.stopPullDownRefresh()
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	// 主卡片信息
	.card-mainInfo{
		background-color: #50a3d9;
		width: 700rpx;
		height: 290rpx;
		margin: 20rpx auto;
		border-radius: 10rpx;
		display: flex;
		flex-direction: column;
		box-shadow: 0rpx 0rpx 2rpx 1rpx rgba(0, 0, 0, 0.2);
		
		//卡内金额、过度金额、卡状态
		.line1{
			display: flex;
			margin: 20rpx 0 0 30rpx;
			margin-top: 35rpx;
			.card-money{
				.card-money1{
					color: white;
					font-size: 25rpx;
				}
				.card-money2{
					color: white;
					font-weight: bold;
					margin-top: 5rpx;
				}
			}
			.cross-money{
				margin: 0rpx 0 0 65rpx;
				.cross-money1{
					color: white;
					font-size: 25rpx;
				}
				.cross-money2{
					color: white;
					font-weight: bold;
					margin-top: 5rpx;
				}
			}
			.card-mode{
				margin: 0rpx 0 0 65rpx;
				.card-mode1{
					color: white;
					font-size: 25rpx;
				}
				.card-mode2{
					color: white;
					font-weight: bold;
					margin-top: 5rpx;
				}
			}
		}
		//实际金额
		.line2{
			margin: 35rpx 0 0 25rpx;
			.real-money{
				.real-money1{
					color: white;
					font-size: 25rpx;
				}
				.real-money2{
					//color: #ffff00;
					background-image: linear-gradient(45deg,rgb(232,229,28) 10%,rgb(23,215,232) 35%);
					-webkit-background-clip: text;
					-webkit-text-fill-color: transparent;
					font-weight: bold;
					margin-top: 5rpx;
					font-size: 55rpx;
					
				}
			}
		}	
	}
	//功能区
	.capabilitie{
		width: 700rpx;
		margin: 20rpx auto;
		//background-color: rgba(89, 170, 232, 0.2);
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		
		.item{
			width: 340rpx;
			height: 120rpx;
			background-color: #fff;
			border-radius: 10rpx;
			box-shadow: 0 3rpx 3rpx 3rpx rgba(0, 0, 0, 0.1);
			margin: 30rpx 0 0 0rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			
			.fun-img{
				width: 50rpx;
				height: 50rpx;
				margin-left: 20rpx;
			}
			.text-item{
				color: rgba(0, 0, 0, 0.5);
				font-weight: bold;
				
			}
			.arrow-right-img{
				width: 30rpx;
				height: 30rpx;
				margin-right: 20rpx;
			}
		}
	}
</style>
